<?php
require 'config.php';
require CAMINHO_SERVIDOR . 'funcoes.php';
require 'login.class.php';
$login = new Login();

if ($login->verificar(URL . 'index.php')) {
    if (!empty($_POST["id_campeonato"])) {
        $query = "SELECT usu.nome,SUM(p.pontos) as pts,c.descricao 
                    FROM tb_pontuacao p INNER JOIN tb_usuario usu ON usu.id_usuario=p.id_usuario
                    INNER JOIN tb_apostas a ON a.id_aposta=p.id_aposta
                    INNER JOIN tb_jogos j ON j.id_jogo=a.id_jogo
                    INNER JOIN tb_campeonato c ON c.id_campeonato=j.id_campeonato 
                    WHERE c.id_campeonato=" . $_POST["id_campeonato"] . "  GROUP BY p.id_usuario ORDER BY pts DESC";
        $ranking = $login->executaQueryAll($query);
        //funcoes::debuga($query);
    }
}
?>
<html>
    <head>
        <title>Ranking</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="bootstrap/css/classes.css">
    </head>
    <body>
        <div class="container">
            <div class="container-fluid margin10px">
                <a class="btn btn-info" href="<?php echo URL_ADMIN; ?>" >Início</a>
            </div>
            <?php if (!empty($ranking)) {
                ?>
                <div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <h3 class="panel-title text-center">Ranking <?php echo $ranking[0]["descricao"]; ?></h3>
                        </div>
                        <div class="panel-body">
                            <table class="table table-bordered">

                                <thead>
                                    <tr class="info">
                                        <th class="text-center">Posição</th>
                                        <th class="text-center">Nome</th>
                                        <th class="text-center">Pontos</th>
                                    </tr>
                                </thead>
                                <?php
                                $cont = 0;
                                foreach ($ranking as $i => $r) {
                                    ?> 
                                    <tbody>
                                        <?php
                                        if ($cont % 2 == 0) {
                                            $classe = "active";
                                        } else {
                                            $classe = "warning";
                                        }
                                        ?>
                                        <tr class="<?php echo $classe; ?>">
                                            <td class="text-center"><?php echo ++$cont; ?> º Lugar</td>
                                            <td class="text-center"><?php echo $r['nome']; ?></td>
                                            <td class="text-center"><?php echo $r['pts']; ?></td>
                                        </tr>
                                    </tbody>
                                    <?php
                                }
                                ?>
                            </table>
                        </div>
                    </div>
                </div>
            <?php } else {
                ?>
                <div class="container-fluid col-md-8 col-md-offset-2">
                    <div class="alert alert-info text-center">
                        <strong>Atenção: </strong>Não existe ranking para esse campeonato!
                    </div>
                </div>
            <?php }
            ?>
        </div>
    </body>
</html>
